package service

import (
	"github.com/gin-gonic/gin"
	"net/http"
	"resr_fun-api/basic/config"
	__ "resr_fun-api/basic/proto"
	"resr_fun-api/handler/model"
	"resr_fun-api/pkg"
)

func SendSms(c *gin.Context) {
	var form model.SendSmsForm

	if err := c.ShouldBind(&form); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code": 400,
			"msg":  "验证失败",
		})
		return
	}

	code := pkg.RandINTN()

	pkg.SendSms(form.Mobile, code)

	sms, err := config.IQiYiClient.SendSms(c, &__.SendSmsReq{
		Mobile: form.Mobile,
		Source: form.Source,
		Code:   code,
	})
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code": 400,
			"msg":  "发送失败",
			"data": err.Error(),
		})
		return
	}

	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"msg":  "发送成功",
		"data": sms,
	})

}

func RegisterUser(c *gin.Context) {
	var form model.CreateUserForm

	if err := c.ShouldBind(&form); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code": 400,
			"msg":  "验证失败",
		})
		return
	}

	user, err := config.IQiYiClient.CreateUser(c, &__.CreateUserReq{
		Mobile:   form.Mobile,
		Password: form.Password,
		Nickname: form.Nickname,
		Sms:      form.Sms,
	})
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code": 400,
			"msg":  "注册失败",
			"data": err.Error(),
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"msg":  "注册成功",
		"data": user,
	})

}

func LoginUser(c *gin.Context) {
	var form model.LoginUserForm

	if err := c.ShouldBind(&form); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code": 400,
			"msg":  "验证失败",
		})
		return
	}
	user, err := config.IQiYiClient.LoginUser(c, &__.LoginUserReq{
		Mobile: form.Mobile,
		Sms:    form.Sms,
	})
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code": 400,
			"msg":  "登陆失败",
			"data": err.Error(),
		})
		return
	}

	token := pkg.CreateJwtToken(user.UserId)
	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"msg":  "登陆成功",
		"data": token,
	})
}

func AddUserVideoLog(c *gin.Context) {
	var form model.AddUSerVideoIngoForm

	if err := c.ShouldBind(&form); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code": 400,
			"msg":  "验证失败",
		})
		return
	}

	ingo, err := config.IQiYiClient.AddUSerVideoIngo(c, &__.AddUSerVideoIngoReq{
		UserId:  int64(form.UserId),
		VideoId: int64(form.VideoId),
	})
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code": 400,
			"msg":  "添加失败",
			"data": err.Error(),
		})
		return
	}

	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"msg":  "添加成功",
		"data": ingo,
	})
}

func AddUserLikeVideo(c *gin.Context) {
	var form model.AddUserLikeVideoForm

	if err := c.ShouldBind(&form); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code": 400,
			"msg":  "验证失败",
		})
		return
	}

	video, err := config.IQiYiClient.AddUserLikeVideo(c, &__.AddUserLikeVideoReq{
		UserId:  int64(form.UserId),
		VideoId: int64(form.VideoId),
	})
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code": 400,
			"msg":  "添加失败",
			"data": err.Error(),
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"msg":  "添加成功",
		"data": video,
	})

}

func GetUserLikeVideo(c *gin.Context) {
	var form model.GetUserLikeVideoForm

	if err := c.ShouldBind(&form); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code": 400,
			"msg":  "验证失败",
		})
		return
	}

	video, err := config.IQiYiClient.GetUserLikeVideo(c, &__.GetUserLikeVideoReq{
		UserId: int64(form.UserId)})
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code": 400,
			"msg":  "查看失败",
			"data": err.Error(),
		})
		return
	}

	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"msg":  "查看成功",
		"data": video,
	})
}

func GetUserVipList(c *gin.Context) {
	list, err := config.IQiYiClient.GetUserVipList(c, &__.GetUserVipListReq{})
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code": 400,
			"msg":  "查看失败",
			"data": err.Error(),
		})
		return
	}

	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"msg":  "查看成功",
		"data": list,
	})
}
